package api

import (
	"context"
	"github.com/gin-gonic/gin"
	"kuangstudy-mall/apis/userrelation-web/microservices"
	context2 "kuangstudy-mall/apis/userrelation-web/models/context"
	"kuangstudy-mall/apis/userrelation-web/proto"
	"kuangstudy-mall/apis/userrelation-web/validation"
)

type UserFavApi struct {
	BaseApi
}

/*
*
查询用户收藏列表
*/
func (api *UserFavApi) FindFavDetail(ctx *gin.Context) {
	favContext := context2.UserFavContext{}
	err := ctx.ShouldBindJSON(&favContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, favContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	userFavRequest := proto.UserFavRequest{
		PageNo:   favContext.PageNo,
		PageSize: favContext.PageSize,
		UserId:   userId,
	}
	userFavListResponse, err := microservices.GetNacosUserFavClient().FindFavDetail(context.Background(), &userFavRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(userFavListResponse, ctx)
}

/*
*
保存收藏
*/
func (api *UserFavApi) SaveUserFav(ctx *gin.Context) {
	userFavSaveContext := context2.UserFavSaveContext{}
	err := ctx.ShouldBindJSON(&userFavSaveContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userFavSaveContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	userSaveFavRequest := proto.UserSaveFavRequest{
		UserId:  userId,
		GoodsId: userFavSaveContext.GoodsId,
	}

	userSaveFavResponse, err := microservices.GetNacosUserFavClient().SaveUserFav(context.Background(), &userSaveFavRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(userSaveFavResponse, ctx)
}

/*
*取消收藏
 */
func (api *UserFavApi) CancelUserFav(ctx *gin.Context) {
	userFavSaveContext := context2.UserFavSaveContext{}
	err := ctx.ShouldBindJSON(&userFavSaveContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userFavSaveContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	userSaveFavRequest := proto.UserSaveFavRequest{
		UserId:  userId,
		GoodsId: userFavSaveContext.GoodsId,
	}

	userSaveFavResponse, err := microservices.GetNacosUserFavClient().CancleUserFav(context.Background(), &userSaveFavRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(userSaveFavResponse, ctx)
}

/*
*删除收货地址信息
 */
func (api *UserFavApi) GetUserFavDetail(ctx *gin.Context) {
	userFavSaveContext := context2.UserFavSaveContext{}
	err := ctx.ShouldBindJSON(&userFavSaveContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userFavSaveContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	userSaveFavRequest := proto.UserSaveFavRequest{
		UserId:  userId,
		GoodsId: userFavSaveContext.GoodsId,
	}

	userSaveFavResponse, err := microservices.GetNacosUserFavClient().GetUserFavDetail(context.Background(), &userSaveFavRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(userSaveFavResponse, ctx)
}
